//
//  PersonCollectionViewCell.m
//  DemoV2
//
//  Created by 董一龙 on 2022/6/25.
//
#import "../../Common.h"
#import "PersonCollectionViewCell.h"
#import <QuartzCore/QuartzCore.h>


@implementation PersonCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        CGSize size = self.frame.size;
        CGFloat width = size.width;
        CGFloat height = size.height;
        
        // self
        self.backgroundColor = [UIColor purpleColor];
        self.userInteractionEnabled=  YES;
        
        // imageView
        self.imageView = [[UIImageView alloc] init];
        [self.imageView setImage:kGetImage(@"kafei.png")];
        CGFloat imageWH = (height - 10)/2;
        self.imageView.frame = CGRectMake((width - imageWH)/2, 10, imageWH, imageWH);
        self.imageView.backgroundColor = [UIColor greenColor];
        
        // titleLabel
        CGFloat labelWidth = width;
        CGFloat labelHeight = (height - 20) / 2;
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10 + imageWH + 10, labelWidth, labelHeight)];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        // [self.titleLabel sizeToFit];
        self.titleLabel.backgroundColor = [UIColor lightGrayColor];
        
        // cancelButton
        // 希望可以子view超过父view，但是有一些问题，所以没有继续做
        // self.cancelbButton = [[UIButton alloc] initWithFrame:CGRectMake(width -10, -10, 20, 20)];
        CGFloat buttonWidth = 20;
        self.cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(width - 25, 5, buttonWidth, buttonWidth)];
        self.cancelButton.backgroundColor = [UIColor redColor];
        [self.cancelButton setTitle:@"x" forState:UIControlStateNormal];
        self.cancelButton.hidden = YES; // 设置隐藏
//        self.cancelButton.userInteractionEnabled=  YES;
        MMViewBorderRadius(self.cancelButton, buttonWidth / 3, 2, [UIColor blackColor]);
        // TODO: 不知道为什么单独展示cell这个设置圆角不能生效, 但是如果放到collectionView里圆角就生效了
        MMViewBorderRadius(self, 8, 0, [UIColor blackColor]);
        
        [self addSubview:self.imageView];
        [self addSubview:self.titleLabel];
        [self addSubview:self.cancelButton];
        
//        self.translatesAutoresizingMaskIntoConstraints = NO;
//        self.imageView.translatesAutoresizingMaskIntoConstraints = NO;
//        self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
//        self.cancelButton.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return self;
}

- (void)beginAnimation{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    animation.fromValue = [NSNumber numberWithFloat:-M_PI/45];
    animation.toValue = [NSNumber numberWithFloat:M_PI/45];
    animation.duration = 0.17;
    animation.repeatCount = MAXFLOAT;
    animation.autoreverses = YES;
    [self.layer addAnimation:animation forKey:@"editAnimation"];
    self.cancelButton.hidden = NO;
}

- (void)stopAnimation{
    // 之前用的是removeAll结果把collectionView的动画也给remove了，造成了bug
    [self.layer removeAnimationForKey:@"editAnimation"];
    self.cancelButton.hidden = YES;
}

- (void)initUI{
    
}

@end
